package cn.frh.user.controller;

import cn.frh.basic.util.AjaxResult;
import cn.frh.basic.util.PageList;
import cn.frh.user.domain.Logininfo;
import cn.frh.user.query.LogininfoQuery;
import cn.frh.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
@Api(tags = "登录信息接口",description = "登录信息接口详细描述")
public class LogininfoController {

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Logininfo findById(@PathVariable("id") Long id){
        return logininfoService.queryById(id);
    }
    //查询所有数据
    @GetMapping("/list")
    public List<Logininfo> findAll(){
        return logininfoService.queryAll();
    }

    @PostMapping("/page")
    public PageList<Logininfo> queryPage(@RequestBody LogininfoQuery query){
        return logininfoService.queryPage(query);
    }
    @DeleteMapping("/{id}")
    public AjaxResult removeByUd(@PathVariable("id") Long id){
        try {
            logininfoService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "登录信息添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Logininfo logininfo){
        try {
            if (logininfo.getId() == null){
                logininfoService.add(logininfo);

            }else {
                logininfoService.update(logininfo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞 操作失败");
        }
    }
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            logininfoService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }
}
